<?php
/**
 * Created by 王跃达.
 * User: 小马出行
 * Date: 2017/8/24
 * Time: 16:17
 */

namespace app\api\logic;



use app\api\model\DriverModel;
use think\Db;
use think\Model;

class DriverLogic extends Model
{

    public function __construct()
    {
        parent::__construct();

    }

    /**
     * 根据手机获取司机信息
     * @param $phone
     * @param $string
     * @return mixed
     */
    public function getDriverByPhone($phone, $string)
    {
        $driverModel=new DriverModel();
        return $driverModel->getDriverByPhone($phone, $string);
    }

    /**
     * 计算失败次数
     * @param $driver_info
     */
    public function freezeUser($driver_info)
    {
        $data['driver_id']=$driver_info["driver_id"];
        $data['error_time']=time();
        if($data['error_time']-$driver_info['error_time']>300){
            $driver_info['wrong_count']=0;
        }
        $data['wrong_count']=$driver_info['wrong_count']+1;
        if($data['wrong_count']>=5){
            $data['can_time']=time()+3600;
        }
        $driverModel=new DriverModel();
        return $driverModel->UpdateDriver($data);
    }

    /**
     * 司机登陆
     * @param $driver_info
     * @return string
     */
    public function driverLogin($driver_info)
    {
        $driver_info['last_login_time']=time();
        $driver_info['token']=md5(date("YmdHis",time()).rand(10000,99999));
        $driverModel=new DriverModel();
        $driverModel->UpdateDriver($driver_info);
        return $driver_info['token'];
    }

    /**
     * 修改密码
     * @param $pass
     * @param $driver_id
     */
    public function forgerPass($pass, $driver_id)
    {
        $data['driver_pass']=md5($pass);
        $data['driver_id']=$driver_id;
        $data['wrong_count']=0;
        $data['can_time']=0;
        $driverModel=new DriverModel();
        $driverModel->UpdateDriver($data);
    }

    /**
     * 创建新司机
     * @param $driver_phone
     * @param $password
     * @return mixed
     */
    public function CreateDriver($driver_phone,$password)
    {
        $driverModel=new DriverModel();
        return $driverModel->CreateDriver($driver_phone,$password);
    }

    /**
     * 根据id获取司机信息
     * @param $id
     * @param $string
     * @return mixed
     * */
    public function getDriverByDid($id,$string)
    {
        $driverModel=new DriverModel();
        return $driverModel->getDriverByDid($id,$string);
    }

    /**
     * 司机ID，所得的钱
     * @param $driver_id,$money
     * @return mixed
     * */
    public function addDriveMoney($driver_id,$money,$common_id)
    {
        $driverModel=new DriverModel();
        return $driverModel->addDriveMoney($driver_id,$money,$common_id);
    }

    /*
     * 更新司机信息
     * @param $data
     * */
    public function updateDriver($data)
    {
        $driverModel=new DriverModel();
        return $driverModel->UpdateDriver($data);
    }

    /*
     * 记录司机登录日志
     * @param $id
     * @param $data
     * */
    public function driver_log($id,$data)
    {
        $data['driver_id'] = $id;
        $data['login_time'] = time();
        Db::name('login_log_driver')->insert($data);
    }

    /**
     * 获取司机不同评分的订单数量
     * @param $map
     * @return array
     * */
    public function getScoreNum($map)
    {
        if(empty($map)){
            return array('status'=>0,'msg'=>'缺少条件');
        }
        $count = db('comment')->where($map)->count();
        return array('status'=>1,'msg'=>'ok','data'=>$count);
    }

    /*
     * 获取司机工作区域
     * @param $did
     * @param $string
     * @return array
     * */
    public function getWorkAreaByDid($did,$string='work_province')
    {
        if(!in_array($string,array_keys(config('work_area')))){
            $string = 'work_province';
        }
        $driver = new DriverModel();
        $res = $driver->getWorkArea($did,$string);
        if($res===false){
            return array('status'=>0,'msg'=>'查询错误');
        }
        return array('status'=>1,'msg'=>'ok','data'=>$res);
    }

    /*
     * 获取司机审核失败原因
     * @param $did
     * @return array
     * */
    public function getAuthFailReasonByDid($did)
    {
        $driver = new DriverModel();
        $map['driver_id'] = $did;
        $res = $driver->getAuthFailReason($map);
        if($res===false){
            return array('status'=>0,'msg'=>'查询错误');
        }
        return array('status'=>1,'msg'=>'ok','data'=>$res);
    }
}